﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

using ES.Client.Expression.Field;

namespace ES.Client
{
    public enum SortDirection
    {
        ASC,
        DESC
    }

    public class SorterItem
    {
        public string Field { get; set; }

        public SortDirection Direction { get; set; }
    }

    public class SortParameter<T> where T : class
    {
        internal List<SorterItem> Items = new List<SorterItem>();

        public SortParameter()
        {

        }

        public SortParameter(string field, SortDirection direction)
        {
            this.Items.Add(new SorterItem()
            {
                Field = field,
                Direction = direction
            });
        }

        public SortParameter(Expression<Func<T, object>> expression, SortDirection direction)
        {
            this.Items.Add(new SorterItem()
            {
                Direction = direction,
                Field = new ConvertParser().Parse(expression)
            });
        }

        public SortParameter<T> Ascending(string field)
        {
            this.Items.Add(new SorterItem()
            {
                Field = field,
                Direction = SortDirection.ASC
            });

            return this;
        }

        public SortParameter<T> Ascending(Expression<Func<T, object>> expression)
        {
            this.Items.Add(new SorterItem()
            {
                Direction = SortDirection.ASC,
                Field = new ConvertParser().Parse(expression)
            });

            return this;
        }

        public SortParameter<T> Descending(string field)
        {
            this.Items.Add(new SorterItem()
            {
                Field = field,
                Direction = SortDirection.DESC
            });

            return this;
        }

        public SortParameter<T> Descending(Expression<Func<T, object>> expression)
        {
            this.Items.Add(new SorterItem()
            {
                Direction = SortDirection.DESC,
                Field = new ConvertParser().Parse(expression)
            });

            return this;
        }
    }
}
